<?php


namespace app\admin\model;


use GatewayWorker\Lib\Gateway;
use think\Db;
use think\facade\Cache;
use think\facade\Log;
use think\Model;
use Workerman\Lib\Timer;

class RobotAction extends Model
{
    public static function up($id, $text, $status)
    {
        $res = RobotAction::where("id", $id)->data(['text' => $text, "status" => $status])->update();
        if (!$res) {
            return "添加失败";
        }
        return "";
    }

    public static function li()
    {
        $lc = RobotAction::count();
        $list = RobotAction::select();
        return listDate($list, $lc);
    }

    public static function actionToUserOne($userNo, $staffNo, $act, $dataAct = "")
    {
        $action_to_user = "action_to_user";
        $handle = Cache::store('default')->handler();
        $handle->lPush($action_to_user,$userNo."-".$staffNo."-".$act."-".$dataAct);
    }

    // 行为
    public static function actionToUser(): string
    {
        $action_to_user = "action_to_user";
        $handle = Cache::store('default')->handler();
        if (!$handle->exists($action_to_user) && $handle->lLen($action_to_user) <= 0){
            return "";
        }
        $dataStr = $handle->rPop($action_to_user);
        list($userNo, $staffNo, $act, $dataAct) = explode("-",$dataStr);


        $data = RobotAction::where("title", $act)->value("text");
        if (!$data && !$dataAct) {
            Log::info("actionToUser:消息不存在");
            return "消息不存在~";
        }
        if ($dataAct) {
            $data = $dataAct;
        }
        $u = Db::table("ic_user")->where("no", $userNo)->find();
        if (empty($u)) {
            Log::info("actionToUser:用户不存在");
            return "";
        }
        $isMen = 0;
        if ($u) {
            $isMen = User::getIsMen($u["mobile"]);
        }
        $s = Db::table("ic_staff")->where("staff_no", $staffNo)->find();
        if (!$s) {
            Log::info("actionToUser:客服不存在");
            return "";
        }
        // 发送标识
        $toUidNo = "use_" . $u["no"];
        $seUidNo = "sta_" . $s["staff_no"];

        // 对话提示语
        $sendToNo = "user";

        // 用户id
        $user = $u["id"];
        $staff = $s["id"];
        // 添加会话
        list($id, $type) = Chat::add($u, $user, $staff, $s, "", $data, $sendToNo);

        if ($id) {
            // 记录
            $chatLog = ChatLog::addLes(0, $id, $user, $staff, $data, "", $sendToNo, $seUidNo, $s["staff_no"], $u["no"]);
            //获取信息
            $list = ChatLog::alias('cl')
                ->field("
             IFNULL(ss.url,'') as store_url,IFNULL(ss.title,'') as store_title,IFNULL(ss.img,'') as store_img,IFNULL(ss.address,'') as store_address,
            IFNULL(ss.industry,'') as store_industry,IFNULL(ss.cost,'') as store_cost,IFNULL(ss.pv,'') as store_pv,IFNULL(ss.rent,'') as store_rent,
            IFNUll(ss.square,'') as store_square,cl.create_time")
                ->leftJoin("ic_user_pv_log ss", "ss.id = cl.store_id")
                ->where("cl.chat_id", $id)->order("cl.id desc")
                ->find()->toArray();
            $js = array(
                "chat_type" => 0,
                'type' => "robot",
                'client_id' => '',
                'chat_id' => $id,
                "chat_log_id" => $chatLog["id"],
                'to_user_no' => $toUidNo,
                'send_user_no' => $seUidNo,
                "info" => $data,
                "sys_time" => "",
                "send_to" => "user",
                "user_type" => $isMen // 会员类型
            );

            $sendInfo = array_merge($js, $list);

            $mess = wsJson("success", $sendInfo);
            Log::info("actionToUser:{$toUidNo}-{$mess}");

            // 对应端口号 延迟1
            Gateway::$registerAddress = \think\facade\Env::get("GATEWAY.REGIS_ADDRESS");
            Log::info("actionToUser:延迟1" . \think\facade\Env::get("GATEWAY.REGIS_ADDRESS") . $toUidNo . $mess);
            Gateway::sendToUid($toUidNo, $mess);
        } else {
            Log::info("actionToUser:提示失败,房间不存在");
        }
        return "";
    }
}